<?php

namespace app\common\library;

use think\facade\Db;

class TableHand
{
    /**
     * 获取字段注释
     * @param $comment
     * @param $default
     * @return array
     */
    public static function getCommentToArray($comment='',$default='')
    {
        if(empty($comment)){
            return [$default,[]];
        }
        $comment = trim($comment);
        $comment = explode(':',$comment);
        $comment_name = $comment[0];
        if(empty($comment_name)){
            $comment_name = $default;
        }
        if(empty($comment[1])){
            return [$comment_name,[]];
        }else{
            $comment_value = explode(',',$comment[1]);
            $values = [];
            foreach ($comment_value as $k=>$v){
                $v = explode('=',trim($v));
                if(count($v)==1){
                    $values[$v[0]] = $v[0];
                }else{
                    $values[$v[0]] = $v[1];
                }
            }
            return [$comment_name,$values];
        }
    }

    /**
     * 获取表字段信息
     * @param $table_name
     * @return array
     */
    public static function getTableFields($table_name)
    {
        $database_name = get_db_config('NAME');
        $prefix = get_db_config('PREFIX');
        if(strpos($table_name,$prefix)===false){
            $table_name = $prefix.$table_name;
        }
        $sql = "SELECT TABLE_SCHEMA,COLUMN_KEY,COLUMN_COMMENT,TABLE_NAME,COLUMN_NAME,DATA_TYPE,IS_NULLABLE,COLUMN_DEFAULT,CHARACTER_MAXIMUM_LENGTH,NUMERIC_PRECISION,NUMERIC_SCALE,ORDINAL_POSITION FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '{$database_name}' AND TABLE_NAME = '{$table_name}';";
        $tableInfo = Db::query($sql);
        $allFiles = [];
        foreach ($tableInfo as $key => $value){
            $nvalue = [];
            foreach ($value as $k => $v){
                $nvalue[strtolower($k)] = $v;
            }
            $tableInfo[$key] = $nvalue;
        }
        foreach ($tableInfo as $value){
            $column_comment = $value['column_comment'];
            list($comment,$option) = self::getCommentToArray($column_comment,ucfirst($value['column_name']));
            $allFiles[] = [
                'name'=>$value['column_name'],
                'type'=>$value['data_type'],
                'is_null'=>$value['is_nullable'],
                'default'=>$value['column_default'],
                'length'=>$value['character_maximum_length'],
                'is_pk'=>$value['column_key']=='PRI'?1:0,
                'comment'=>$comment,
                'option'=>$option
            ];
        }
        return $allFiles;
    }
    public static function getTableOneField($table_name,$field_name)
    {
        $database_name = get_db_config('NAME');
        $prefix = get_db_config('PREFIX');
        if(strpos($table_name,$prefix)===false){
            $table_name = $prefix.$table_name;
        }
        $sql = "SELECT TABLE_SCHEMA,COLUMN_KEY,COLUMN_COMMENT,TABLE_NAME,COLUMN_NAME,DATA_TYPE,IS_NULLABLE,COLUMN_DEFAULT,CHARACTER_MAXIMUM_LENGTH,NUMERIC_PRECISION,NUMERIC_SCALE,ORDINAL_POSITION FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '{$database_name}' AND TABLE_NAME = '{$table_name}' AND COLUMN_NAME = '{$field_name}';";
        $tableInfo = Db::query($sql);
        $allFiles = [];
        foreach ($tableInfo as $key => $value){
            $nvalue = [];
            foreach ($value as $k => $v){
                $nvalue[strtolower($k)] = $v;
            }
            $tableInfo[$key] = $nvalue;
        }
        foreach ($tableInfo as $value){
            $column_comment = $value['column_comment'];
            list($comment,$option) = self::getCommentToArray($column_comment,ucfirst($value['column_name']));
            $allFiles[] = [
                'name'=>$value['column_name'],
                'type'=>$value['data_type'],
                'is_null'=>$value['is_nullable'],
                'default'=>$value['column_default'],
                'length'=>$value['character_maximum_length'],
                'is_pk'=>$value['column_key']=='PRI'?1:0,
                'comment'=>$comment,
                'option'=>$option
            ];
        }
        return $allFiles[0];
    }
}